package com.ruoyi.business.controller;

import com.ruoyi.business.domain.EquipmentTypes;
import com.ruoyi.business.domain.EquipmentVariables;
import com.ruoyi.business.service.IEquipmentTypesService;
import com.ruoyi.business.service.IEquipmentVariablesService;
//import com.ruoyi.business.vo.StripTemMonitorVO;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 存储设备变量参数信息的Controller
 * 
 * @author ldkj
 * @date 2025-02-27
 */
@Api(tags = "设备变量管理")
@RestController
@RequestMapping("/deviceInfo/variables")
public class EquipmentVariablesController extends BaseController
{
    @Autowired
    private IEquipmentVariablesService equipmentVariablesService;
    
    @Autowired
    private IEquipmentTypesService equipmentTypesService;

    /**
     * 查询存储设备变量参数信息的列表
     */
    @ApiOperation("查询设备变量列表")
    @PreAuthorize("@ss.hasPermi('deviceInfo:variables:list')")
    @GetMapping("/list")
    public TableDataInfo list(EquipmentVariables equipmentVariables)
    {
        startPage();
        List<EquipmentVariables> list = equipmentVariablesService.selectEquipmentVariablesList(equipmentVariables);
        return getDataTable(list);
    }

    /**
     * 导出存储设备变量参数信息的列表
     */
    @ApiOperation("导出设备变量列表")
    @PreAuthorize("@ss.hasPermi('deviceInfo:variables:export')")
    @Log(title = "存储设备变量参数信息的", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EquipmentVariables equipmentVariables)
    {
        List<EquipmentVariables> list = equipmentVariablesService.selectEquipmentVariablesListForExport(equipmentVariables);
        ExcelUtil<EquipmentVariables> util = new ExcelUtil<EquipmentVariables>(EquipmentVariables.class);
        // 排除delFlag字段，不在导出中包含"是否删除"列
        util.hideColumn("delFlag");
        util.exportExcel(response, list, "设备变量数据");
    }

    /**
     * 获取存储设备变量参数信息的详细信息
     */
    @ApiOperation("获取设备变量详细信息")
    @PreAuthorize("@ss.hasPermi('deviceInfo:variables:query')")
    @GetMapping(value = "/{variableId}")
    public AjaxResult getInfo(@ApiParam("变量ID") @PathVariable("variableId") Long variableId)
    {
        return success(equipmentVariablesService.selectEquipmentVariablesByVariableId(variableId));
    }

    /**
     * 新增存储设备变量参数信息的
     */
    @ApiOperation("新增设备变量")
    @PreAuthorize("@ss.hasPermi('deviceInfo:variables:add')")
    @Log(title = "存储设备变量参数信息的", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@ApiParam("设备变量信息") @RequestBody EquipmentVariables equipmentVariables)
    {
        return toAjax(equipmentVariablesService.insertEquipmentVariables(equipmentVariables));
    }

    /**
     * 修改存储设备变量参数信息的
     */
    @ApiOperation("修改设备变量")
    @PreAuthorize("@ss.hasPermi('deviceInfo:variables:edit')")
    @Log(title = "存储设备变量参数信息的", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@ApiParam("设备变量信息") @RequestBody EquipmentVariables equipmentVariables)
    {
        return toAjax(equipmentVariablesService.updateEquipmentVariables(equipmentVariables));
    }

    /**
     * 删除存储设备变量参数信息的
     */
    @ApiOperation("删除设备变量")
    @PreAuthorize("@ss.hasPermi('deviceInfo:variables:remove')")
    @Log(title = "存储设备变量参数信息的", businessType = BusinessType.DELETE)
	@DeleteMapping("/{variableIds}")
    public AjaxResult remove(@ApiParam("变量ID数组") @PathVariable Long[] variableIds)
    {
        return toAjax(equipmentVariablesService.deleteEquipmentVariablesByVariableIds(variableIds));
    }





    /**
     * 导入设备变量数据
     */
    @ApiOperation("导入设备变量数据")
    @Log(title = "设备变量", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('deviceInfo:variables:import')")
    @PostMapping("/importData")
    public AjaxResult importData(@ApiParam("Excel文件") MultipartFile file, @ApiParam("是否更新支持") boolean updateSupport) throws Exception
    {
        ExcelUtil<EquipmentVariables> util = new ExcelUtil<EquipmentVariables>(EquipmentVariables.class);
        List<EquipmentVariables> equipmentVariablesList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = equipmentVariablesService.importEquipmentVariables(equipmentVariablesList, updateSupport, operName);
        return success(message);
    }

    /**
     * 获取设备类型下拉选项
     */
    @ApiOperation("获取设备类型下拉选项")
    @GetMapping("/getEquipmentTypeOptions")
    public AjaxResult getEquipmentTypeOptions()
    {
        List<EquipmentTypes> equipmentTypes = equipmentTypesService.selectEquipmentTypesList(new EquipmentTypes());
        return success(equipmentTypes);
    }

    /**
     * 下载导入模板
     */
    @ApiOperation("下载设备变量导入模板")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        // 获取所有设备类型名称
        List<EquipmentTypes> equipmentTypes = equipmentTypesService.selectEquipmentTypesList(new EquipmentTypes());
        String[] equipmentTypeNames = equipmentTypes.stream()
                .map(EquipmentTypes::getEquipmentTypeName)
                .toArray(String[]::new);
        
        // 创建一个临时的 EquipmentVariables 类用于模板生成
        ExcelUtil<EquipmentVariables> util = new ExcelUtil<EquipmentVariables>(EquipmentVariables.class);
        util.importTemplateExcel(response, "设备变量导入模板");
    }

}
